﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Taschenrechner {
    public partial class UserInterface : Form {
        private Controler controler;
        public UserInterface() {
            InitializeComponent();
            controler = new Controler();
            BindToControler();
            SetErrorMessage(String.Empty);
        }

        private void BindToControler() {
            Binding displayBinding = new Binding("Text", this.controler, "Display");
            this.display.DataBindings.Add(displayBinding);
            Binding resultBinding = new Binding("Text", this.controler, "Result");
            this.result.DataBindings.Add(resultBinding);
        }

        private void UserInterface_KeyPress(object sender, KeyPressEventArgs e) {

            if (e.KeyChar == (char)Keys.Enter) {
                MessageBox.Show(e.KeyChar.ToString());
            }

            char charToProcess = e.KeyChar;

            switch (charToProcess) {
            case (char)Keys.Escape:
                e.Handled = true;
                controler = null;
                this.Close();
                return;
            //TODO: Form verarbeitet die Enter-Taste nicht !! Warum ??
            case (char)Keys.Return:
                charToProcess = '-';
                break;
            default:
                break;
            }

            ProcessChar(charToProcess);
            e.Handled = true;

        }

        private void Button_Click(object sender, System.EventArgs e) {
            string strToProcess = ((Button)sender).Text;
            char charToProcess = Convert.ToChar(strToProcess);
            ProcessChar(charToProcess);
        }

        //TODO: Funktioon AcceptButton falsch, wenn anderer Button als "="-Button Focus hat !! Warum ??
        //TODO: Selbst nach Erstellen der Funktion AcceptButton_Click keine Veränderung!
        private void AcceptButton_Click(object sender, System.EventArgs e) {
            Button_Click(sender, e);
        }

        private void ProcessChar(char charToProcess) {
            SetErrorMessage( String.Empty );
            try {
                controler.ProcessChar(charToProcess);
            }
            catch (OverflowException e) {
                SetErrorMessage(e.Message);
            }
            catch (DivideByZeroException e) {
                SetErrorMessage(e.Message);
            }
            catch (Exception e) {
                throw e;
            }
        }

        private void ButtonCE_Click(object sender, EventArgs e) {
            controler.ClearEntry();
        }

        private void ButtonC_Click(object sender, EventArgs e) {
            controler.Clear();
        }

        private void SetErrorMessage(string msg) {
            error.Text = msg;
            if (String.IsNullOrEmpty(msg)) {
                error.ResetForeColor();
            }
            else {
                error.ForeColor = Color.Red;
            }
        }

    }
}
